#include <stdio.h>
#include <mysql/mysql.h>

#include "../include/mysql_log.h"

int mysql_log(MYSQL *mysql,log_priority_t priority,const char *format,...){
    char query[1024];
    const char *priority_str;
    switch (priority){
        case LOG_EMERG:{priority_str="emerg";break;}
        case LOG_ALTER:{priority_str="alter";break;}
        case LOG_CRIT:{priority_str="crit";break;}
        case LOG_ERROR:{priority_str="error";break;}
        case LOG_WARNING:{priority_str="warning";break;}
        case LOG_NOTICE:{priority_str="notice";break;}
        case LOG_INFO:{priority_str="info";break;}
        case LOG_DEBUG:{priority_str="debug";break;}
    }

    char msg[1024];
    va_list args;
    
    va_start(args,format);
    vsnprintf(msg,sizeof(msg),format,args);
    va_end(args);

    snprintf(query,sizeof(query),"INSERT INTO log (priority,info) VALUES('%.10s','%.1024s');",priority_str,msg);

    return mysql_query(mysql,query);
}
